package com.agileai.bpm.controller.define;

import com.agileai.bpm.common.ProcessDefineUtil;
import com.agileai.bpm.controller.resource.WfDBConfigSelectHandler;
import com.agileai.bpm.controller.resource.WfDbconfigManageEditHandler;
import com.agileai.bpm.controller.ui.VariableBox;
import com.agileai.bpm.model.DBOperaterType;
import com.agileai.bpm.model.InterceptorModelDocument.InterceptorModel;
import com.agileai.bpm.model.ProcessDefineDocument.ProcessDefine;
import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.StringUtil;

public class WfDBOperatorEditHandler extends WfProcessInfoHandler {
	public ViewRenderer prepareDisplay(DataParam param){
		this.mergeParam(param);
		this.setAttributes(param);
		this.initAttriubtes(param);
		return new LocalRenderer(getPage());
	}
	public ViewRenderer doSaveAction(DataParam param){
		String rspText = null;
		String activityId = param.get("WFA_ID");
		String interceptorId = param.get("interceptorId");
		String positionType = param.get("positionType");
		String isSynchronous = param.get("isSynchronous");
		String sqlTemplate = param.get("sqlTemplate");
		if ("true".equals(isSynchronous)){
			if (!sqlTemplate.toLowerCase().startsWith("select")){
				return new AjaxRenderer("同步的操作类型必须以select开头！");
			}
		}else if ("false".equals(isSynchronous)){
			if (!sqlTemplate.toLowerCase().startsWith("update")
					&& !sqlTemplate.toLowerCase().startsWith("delete")
					&& !sqlTemplate.toLowerCase().startsWith("insert")
					&& !sqlTemplate.toLowerCase().startsWith("merge")
					){
				return new AjaxRenderer("异步的操作操作必须以update、delete、insert、merge之一开头！");
			}
		}
		ProcessDefine processDefine = getProcessDefine();
		InterceptorModel interceptorModel = null;
		if (StringUtil.isNullOrEmpty(interceptorId)){
			interceptorModel = ProcessDefineUtil.createInterceptorModel(processDefine, positionType, activityId);
			String id = KeyGenerator.instance().genKey();
			interceptorModel.setId(id);
		}
		else{
			interceptorModel = ProcessDefineUtil.getInterceptorModel(processDefine, positionType, activityId, interceptorId);
		}
		this.buildInterceptor(interceptorModel, param);
		rspText = SUCCESS;
		return new AjaxRenderer(rspText);
	}
	
	private void buildInterceptor(InterceptorModel interceptorModel,DataParam param){
		DBOperaterType dbOperaterType = interceptorModel.getDBOperater();
		if  (dbOperaterType == null){
			dbOperaterType = interceptorModel.addNewDBOperater();
		}
		String dataSourceCode = param.get("dataSourceCode");
		String interceptorName = param.get("interceptorName");
		String sqlTemplate = param.get("sqlTemplate");
		String variableCode = param.get("variableCode");
		String isSynchronous = param.get("isSynchronous");
		
		dbOperaterType.setDataSource(dataSourceCode);
		dbOperaterType.setUpdateSQL(sqlTemplate);
		interceptorModel.setSynchronous(Boolean.parseBoolean(isSynchronous));
		interceptorModel.setName(interceptorName);
		interceptorModel.setVariableCode(variableCode);
	}
	public ViewRenderer doBackAction(DataParam param){
		this.mergeParam(param);
		return new DispatchRenderer(getHandlerURL(WfInterceptorTypeSelectHandler.class));
	}
	public ViewRenderer doSelectDBLinkAction(DataParam param){
		storeParam(param);
		return new DispatchRenderer(getHandlerURL(WfDBConfigSelectHandler.class));
	}
	
	public ViewRenderer doAddDBLinkAction(DataParam param){
		storeParam(param);
		return new DispatchRenderer(getHandlerURL(WfDbconfigManageEditHandler.class)
				+"&"+OperaType.KEY+"="+OperaType.CREATE
				+"&comeFrom="+WfDbconfigManageEditHandler.class.getSimpleName());
	}
	
	private void initAttriubtes(DataParam param){
		FormSelect formSelect = FormSelectFactory.create("InterceptorType");
		formSelect.setHasBlankValue(false);
		String interceptorType = param.get("interceptorType");
		formSelect.setSelectedValue(interceptorType);
		this.setAttribute("interceptorTypeSelect", formSelect);
		
		String activityId = param.get("WFA_ID");
		String interceptorId = param.get("interceptorId");
		String positionType = param.get("positionType");
		String dataSourceCode = param.get("dataSourceCode");
		
		ProcessDefine processDefine = getProcessDefine();
		if (!StringUtil.isNullOrEmpty(interceptorId)){
			InterceptorModel interceptorModel = ProcessDefineUtil.getInterceptorModel(processDefine
					, positionType, activityId, interceptorId);
			if (StringUtil.isNullOrEmpty(dataSourceCode)){
				dataSourceCode = interceptorModel.getDBOperater().getDataSource();			
			}
			String interceptorName = interceptorModel.getName();
			String sqlTemplate = interceptorModel.getDBOperater().getUpdateSQL();
			String variableCode = interceptorModel.getVariableCode();
			String isSynchronous = String.valueOf(interceptorModel.getSynchronous());
			this.setAttribute("dataSourceCode", dataSourceCode);
			
			this.setAttribute("interceptorName", interceptorName);
			this.setAttribute("sqlTemplate", sqlTemplate);
			this.setAttribute("variableCode", variableCode);
			this.setAttribute("isSynchronous", isSynchronous);
		}
		if (this.getAttribute("isSynchronous")==null){
			this.setAttribute("isSynchronous","false");
		}
		if (!StringUtil.isNullOrEmpty(dataSourceCode)){
			this.setAttribute("dataSourceCode", dataSourceCode);
		}
		String varCode = param.get("varCode");
		if (!StringUtil.isNullOrEmpty(varCode)){
			this.setAttribute("variableCode", varCode);
		}
	}

	public ViewRenderer doShowVariableBoxAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doShowVariableBoxAction();
	}
	
	public ViewRenderer doChangeVariableTypeAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doChangeVariableTypeAction();
	}
	
	public ViewRenderer doSelectVariableAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
//		String activityId = param.get("WFA_ID");
		String varTargetId = param.get("varTargetId");
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		if ("variableCode".equals(varTargetId)){
			return variableSelectBox.doSelectVariableCode();
		}else{
			return variableSelectBox.doSelectVariableAction();			
		}
	}
}